package ru.ipo.dces.plugins.admin.packetregistration.passwordgenerator;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 13.06.11
 * Time: 15:16
 */
public abstract class PasswordGenerator {

    protected final Random random;

    public PasswordGenerator() {
        random = new Random();
    }

    public PasswordGenerator(long randSeed) {
        random = new Random(randSeed);
    }

    protected abstract char[] generateRandomChars(int length);

    public String getWord(int length) {
        return new String(generateRandomChars(length));
    }

    public String getWord(int length, int uppercaseLettersNumber) {
        char[] chars = generateRandomChars(length);
        Integer[] indexes = new Integer[length];
        for (int i = 0; i < length; i++)
            indexes[i] = i;
        List<Integer> indexesAsList = Arrays.asList(indexes);
        Collections.shuffle(indexesAsList, random);
        for (int i = 0; i < uppercaseLettersNumber; i++) {
            Integer index = indexesAsList.get(i);
            chars[index] = Character.toUpperCase(chars[index]);
        }

        return new String(chars);
    }


}
